package com.vae.learn.config;

import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.Binding;

/**
 * RabbitMQ的配置类
 * 消费者: 负责创建队列、交换机，负责监听的代码编写(监听的代码放在ReceivePayNotifyService类)。不需要写确认机制(也就是该类不需要实现ApplicationContextAware接口)的代码
 * 为什么消费者和生产者都需要负责创建一模一样的队列、交换机: 因为避免启动顺序导致队列、交换机还没有被创建出来导致的报错。不用担心重复创建
 */
@Configuration
public class MQNotifyConfig {

    //交换机名称
    public static final String PAYNOTIFY_EXCHANGE_FANOUT = "paynotify_exchange_fanout";
    //消息类型
    public static final String MESSAGE_TYPE = "payresult_notify";
    //队列名称
    public static final String PAYNOTIFY_QUEUE = "paynotify_queue";

    //交换机，且持久化
    @Bean(PAYNOTIFY_EXCHANGE_FANOUT)
    public FanoutExchange paynotify_exchange_fanout() {
        // 三个参数：交换机名称、是否持久化、当没有queue与其绑定时是否自动删除
        return new FanoutExchange(PAYNOTIFY_EXCHANGE_FANOUT, true, false);
    }
    //队列,且持久化
    @Bean(PAYNOTIFY_QUEUE)
    public Queue course_publish_queue() {
        return QueueBuilder.durable(PAYNOTIFY_QUEUE).build();
    }

    //交换机和队列的绑定
    @Bean
    public Binding binding_course_publish_queue(@Qualifier(PAYNOTIFY_QUEUE) Queue queue, @Qualifier(PAYNOTIFY_EXCHANGE_FANOUT) FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }
}